package com.aoip.telemetry.telemetry.core.util;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/7/6 17:10
 */
public class ConvertUtil {

    public static String convertCharToHex(char aChar) {
//        int numericValue = Character.getNumericValue(aChar);
        int numericValue = (int)aChar;
        return Integer.toHexString(numericValue);

    }

    public static String convertStringToHex(String str) {
        char[] chars = str.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char aChar : chars) {
            hex.append(Integer.toHexString((int) aChar));
        }
        return hex.toString();
    }

    public static String convertHexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        //将字符串分割成逐个字节，例如：323537 -> 32 35 37
        char[] chars = hex.toCharArray();
        for (int i = 0; i < chars.length - 1; i += 2) {
            String output = new String(chars, i, 2);
            //转化为十进制整型数
            int decimal = Integer.parseInt(output, 16);
            //转化为十进制整型数对应的字符
            sb.append((char) decimal);
        }
        return sb.toString();
    }

}
